<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\s_commerce_service\Entity\SCommerceServiceLine;
use Drupal\shusy_order_product_first_coupon\Entity\ShusyOrderProductFirstCoupon;
use EntityListWrapper;

/**
 * @OA\Schema(
 *   schema="LineItemProduct",
 *   type="object",
 *   description="LineItemProduct"
 * )
 */
class LineItemProduct extends AbstractLineItem {

  protected function getCoupon() {
    $t = shusy_order_product_first_coupon_load_single(FALSE, [
      'product_standard_id' => $this->wrapper->standard->id->value(),
      'uid' => $this->getUid(),
    ]);
    if ($t instanceof ShusyOrderProductFirstCoupon && $t->status == 1) {
      return ['amount' => $this->convertYuan($t->amount)];
    }
    return FALSE;
  }

  protected function getShippings() {
    $productId = $this->wrapper->standard->product_id->value();
    $tmps = shusy_product_shipping_product_shippings($productId);
    if (empty($tmps)) {
      $tmps = shusy_store_order_shipping_type_options();
    }
    return $tmps;
  }

  protected function getImage() {
    $wrapper = $this->wrapper->standard->product;
    $field_image = $wrapper->field_image->value();
    if (empty($field_image)) {
      $field_images = $wrapper->field_images;
      if ($field_images instanceof EntityListWrapper) {
        foreach ($field_images as $image) {
          $field_image = $image->value();
          break;
        }
      }
    }
    return $this->imageStyleUrl('ProductItemImage', $field_image);
  }

  protected function getName() {
    return $this->wrapper->standard->product->name->value();
  }

  protected function getStandard() {
    return $this->wrapper->standard->standard->value();
  }

  protected function getQty() {
    return $this->entity->qty;
  }

  protected function getAmount() {
    return $this->convertYuan($this->entity->amount);
  }

  protected function getTotal() {
    return $this->convertYuan($this->entity->total);
  }

  protected function getIsService() {
    return $this->wrapper->standard->product->is_service->value();
  }

  /**
   * 售后状态
   * 售后状态 0 未申请 1 审核中 2 处理中 3 已完成 4 待补充信息 5 被拒绝
   */
  protected function getService() {
    $service = 0;
    if (module_exists('s_commerce_service')) {
      $es = entity_load('s_commerce_service_line', FALSE, ['line_item_id' => $this->entity_id]);
      if (!empty($es)) {
        $e = reset($es);
        if ($e instanceof SCommerceServiceLine) {
          //状态 0 待审核 1 待退款 2 已完成 3 拒绝 4 待退货
          switch ($e->status) {
            case 0:
              $service = 1;
              break;
            case 1:
              $service = 2;
              break;
            case 2:
              $service = 3;
              break;
            case 3:
              $service = 0;
              break;
            case 4:
              $service = 4;
              break;
          }
        }
      }
    }
    return $service;
  }

  protected function getServiceLabel() {
    switch ($this->getService()) {
      case 0:
        return '申请售后';
        break;
      case 1:
        return '审核中';
        break;
      case 2:
        return '处理中';
        break;
      case 3:
        return '已处理';
        break;
      case 4:
        return '待退货';
        break;
    }
    return '申请售后';
  }

  protected function getServiceDesc() {
    $desc = '';
    if (module_exists('s_commerce_service')) {
      $es = entity_load('s_commerce_service_line', FALSE, ['line_item_id' => $this->entity_id]);
      if (!empty($es)) {
        $e = reset($es);
        if ($e instanceof SCommerceServiceLine) {
          //状态 0 待审核 1 待退款 2 已完成 3 拒绝 4 待退货
          switch ($e->status) {
            case 3:
              $desc = $e->reason_desc;
              break;
          }
        }
      }
    }
    return $desc;
  }
}
